/*
 *  levelUpGame -  BalanceMeter.cpp
 *  Copyright 2011 W. Reckman. All rights reserved.
 */
#include "ofGraphics.h"
#include "ofMath.h"
#include "BalanceMeter.h"

const int BalanceMeter::METER_WIDTH = 64;
const int BalanceMeter::METER_HEIGHT = 15;


BalanceMeter::BalanceMeter()
: balancePos( 0.0f )
{}

void BalanceMeter::draw() const {
	float dx = pos.x - METER_WIDTH / 2, dy = pos.y - METER_HEIGHT / 2;
	
	ofPushStyle();
	ofFill();
	
	ofSetColor( 255, 63, 0 );
	ofTriangle( dx, dy, dx + METER_WIDTH, dy, dx, dy + METER_HEIGHT );
	ofSetColor( 0, 63, 255 );
	ofTriangle( dx, dy + METER_HEIGHT, dx + METER_WIDTH, dy + METER_HEIGHT, dx + METER_WIDTH, dy );
	ofNoFill();
	ofSetColor( 255, 255, 255 );
	ofRect( dx, dy, METER_WIDTH, METER_HEIGHT );
	
	float lineX = dx + ( balancePos + 0.5f ) * METER_WIDTH;
	ofSetLineWidth( 3 );
	ofLine(lineX, dy - 2, lineX, dy + METER_HEIGHT + 2 );
	
	ofPopStyle();
}


float BalanceMeter::getBalanceFloat() const { return balancePos; }
const ofPoint& BalanceMeter::getPosition() const { return pos; }

//-0.5 (culture) - +0.5 (industry)
void BalanceMeter::setBalanceFloat( float balanceF_ ) {
	balancePos = ofClamp( balanceF_, -0.5f, 0.5f );
}

//0 - 100 -> -0.5 - 0.5
void BalanceMeter::setBalanceIndustry( int balanceInd_ ) {
	float fv = balanceInd_ / 100.0f - 0.5f;
	balancePos = ofClamp( fv, -0.5f, 0.5f );
}

//100 - 0 -> -0.5 - 0.5
void BalanceMeter::setBalanceCulture( int balanceCult_ ) {
	float fv = 1.0f - balanceCult_ / 100.0f - 0.5f;
	balancePos = ofClamp( fv, -0.5f, 0.5f );
}

//0 - 100
void BalanceMeter::addBalanceIndustry( int balanceInd_ ) {
	balancePos += balanceInd_ / 100.0f;
	balancePos = ofClamp( balancePos, -0.5f, 0.5f );
}

//100 - 0
void BalanceMeter::addBalanceCulture( int balanceCult_ ) {
	balancePos -= balanceCult_ / 100.0f;
	balancePos = ofClamp( balancePos, -0.5f, 0.5f );
}

void BalanceMeter::setPosition( const ofPoint& pos_ ) { pos = pos_; }
void BalanceMeter::setPosition( float x_, float y_ ) { pos.x = x_; pos.y = y_; }
